package com.dao;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.model.GameModel;

public class GameDao extends DefaultDao implements Serializable{
	public void salvar(GameModel game){
		EntityManager em = getEntityManager();
		try{
			em.getTransaction().begin();
			em.persist(game);
			em.getTransaction().commit();
		}catch (Exception e) {
			em.getTransaction().rollback();
		}
	}
	public GameModel getGameModel(Integer id){
		GameModel gm = getEntityManager().find(GameModel.class,id);
		return gm;
	}
	public void novoGame(String titulo){
		GameModel g = new GameModel();
		g.setTitulo(titulo);
		salvar(g);
	}
	public List<GameModel> getGames(){
		EntityManager em = getEntityManager();
		try{
			Query q = em.createQuery("select e from GameModel e");
			return q.getResultList();
		}
		finally{
			em.close();
		}
	}
}